package asteroids;


import java.awt.Point;

import javax.swing.JOptionPane;

import geometry.Geometry;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Sprite;
import com.uqbar.vainilla.events.constants.Key;

public class Ship extends AsteroidsComponent {
	
	private Sprite sprite; 
	private int vMax = 10;
	private double thrust = 0;
	private double o = Math.PI/2;//orientacion
	private double hAcc = 0;
	
	
	private final double HDELTA = 0.2;
	private final double ADELTA = 1; 
	
	//variables que se usarian para un movimiento de la nave mas simple
	//private double i = 0;
	//private double j = 0;
	//private int speed = 0;
	
	public Ship(int x,int y) {
		super(x,y);
		this.sprite = Sprite.fromImage("/images/ship.png").scale(0.5);
		this.setAppearance(this.sprite);
	}
	
	@Override
	public void update(DeltaState deltaState) {
		super.update(deltaState);
		
		if(this.collidesAgainstAsteroid()){
			JOptionPane.showMessageDialog(null, "PERDISTE =(");
			System.exit(0);
		}else{
			if(deltaState.isKeyPressed(Key.SPACE)){
				this.shoot();
			}
			if(deltaState.isKeyBeingHold(Key.LEFT)){
				this.sprite = this.sprite.rotate(-this.HDELTA);
				this.setAppearance(this.sprite);
				this.hAcc = this.HDELTA;			
			}else{
				if(deltaState.isKeyBeingHold(Key.RIGHT)){
					this.sprite = this.sprite.rotate(this.HDELTA);
					this.setAppearance(this.sprite);
					this.hAcc = -this.HDELTA;
				}else
					this.hAcc = 0;
			}
			
			if(deltaState.isKeyBeingHold(Key.UP)){
				this.thrust = this.ADELTA;
			}else
				this.thrust = 0;
			
			Point vel1 = Geometry.add(Geometry.polar(this.thrust,this.o),this.getVector());
			
			double l = Geometry.len(vel1);
			if(l>this.vMax)
				this.setVector(Geometry.smult(this.vMax/l, vel1));
			else
				this.setVector(vel1);
			this.o += this.hAcc;		

			this.addWinMod(vel1);
		}
	}

	public void addWinMod(Point vel1){
		this.setX(this.getX()+vel1.getX());// % this.getGame().getDisplayWidth());
		this.setY(this.getY()+vel1.getY());// % this.getGame().getDisplayHeight());
		
		this.checkScreenLimits();
	}
	
	public void shoot() {
		this.getGame().getCurrentScene().addComponent(new Bullet(this));	
	}
	
	public double getO() {
		return o;
	}
	
	
	//MOVIMIENTO DE LA NAVE MAS SIMPLE
//	@Override
//	public void update(DeltaState deltaState) {
//		super.update(deltaState);
//		
//	    if(deltaState.isKeyBeingHold(Key.LEFT)){
//			this.sprite = this.sprite.rotate(-this.HDELTA);
//			this.setAppearance(this.sprite);
//			this.o += -this.HDELTA;
//	    }else{
//			if(deltaState.isKeyBeingHold(Key.RIGHT)){
//				this.sprite = this.sprite.rotate(this.HDELTA);
//				this.setAppearance(this.sprite);
//				this.o += this.HDELTA;
//			}
//	    }
//		
//		if(deltaState.isKeyBeingHold(Key.UP))
//			this.speed++;	
//		
//		this.i = Math.cos(this.o);
//		this.j = Math.sin(this.o); 
//		
//		double advanced = -this.speed * deltaState.getDelta();
//
//		this.move(this.i * advanced, this.j * advanced);	
//		
//		int winWidth = this.getGame().getDisplayWidth();
//		int winHeight = this.getGame().getDisplayHeight();
//		double x = this.getX();
//		double y = this.getY();
//		if(x>winWidth)
//			this.setX(-this.width);
//		if(x<-this.width)
//			this.setX(winWidth);
//		if(y>winHeight)
//			this.setY(-this.height);
//		if(y<-this.height)
//			this.setY(winHeight);
//	}
	
	

}
